package util;


import java.util.Random;

import net.molchat.binroot.Root;


public class RootTestUtil {

private static Random rand = new Random();

public static final byte[] refRootBytes = new byte[] { 0x07, 0x01, 0x0B, 0x09,
		0x01, 0x16, 0x0C, 0x01, 0x21, 0x12, 0x01, 0x2C, 0x14, 0x01, 0x37, 0x1B,
		0x01, 0x42, 0x1D, 0x01, 0x4D, 0x22, 0x01, 0x58, 0x26, 0x01, 0x63 };

private static int chanseToHaveChild = 50; // Percents
private static int nodeTypeMax = 512;
private static int nodeDataLenMax = 2; // bytes


public static Root makeRefRoot() {

	Root[] r = new Root[9];

	for (int q = 0; q < r.length; ++q) {
		r[q] = new Root();
		r[q].nodeId = q + 1;
		r[q].data = new byte[1];
		r[q].data[0] = (byte) ((q + 1) * 11);
	}


	r[0].addChild(r[1]);

	r[1].addChild(r[2]);
	r[1].addChild(r[3]);


	r[0].addChild(r[4]);


	r[0].addChild(r[5]);

	r[5].addChild(r[6]);
	r[6].addChild(r[7]);
	r[5].addChild(r[8]);

	return r[0];
}


public static Root makeRandomRoot(int nodesMax) {

	Counter counter = new Counter();
	counter.val = nodesMax - 1;

	Root root = makeRandomNode();

	while (counter.val > 0) {
		makeRandomRoot(root, counter);
	}

	return root;
}


private static void makeRandomRoot(Root rootParent, Counter counter) {

	Root root = makeRandomNode();
	rootParent.addChild(root);
	counter.val--;

	// Make childs
	while (counter.val > 0 && (rand.nextInt(100 + 1) <= chanseToHaveChild)) {
		makeRandomRoot(root, counter);
	}
}


private static Root makeRandomNode() {

	Root root = new Root();
	root.nodeId = rand.nextInt(nodeTypeMax);
	int contLen = rand.nextInt(nodeDataLenMax + 1);
	if (contLen > 0) {
		root.data = new byte[contLen];
		rand.nextBytes(root.data);
	}
	return root;
}


public static void main(String[] args) {

	Root root = RootTestUtil.makeRandomRoot(50);
	System.out.println(root);
}


}
